package fr.enssat.tpcloud.service;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;


import fr.enssat.tpcloud.dao.PostDao;
import fr.enssat.tpcloud.dao.UserDao;
import fr.enssat.tpcloud.model.Notif;
import fr.enssat.tpcloud.model.Post;
import fr.enssat.tpcloud.model.User;
import fr.enssat.tpcloud.model.UserType;
import fr.enssat.tpcloud.resource.PostContainer;
import fr.enssat.tpcloud.resource.PostData;

/**
 * 
 * Service REST.
 *
 */
@Path("/post")
public class PostManagerService {

    /**
     * DAO for user.
     */
    private UserDao userDao;

    /**
     * DAO for post.
     */
    private PostDao postDao;

    /**
     * add a new Post.
     * 
     * @param onePost new post to persist.
     * @return 201 for success, 500 for error.
     */
    @POST
    @Consumes("application/json")
    public Response postJsonMessage(PostData onePost) {

        try {
            User user = userDao.findByEmail(onePost.getEmail());

            // if new user : create it
            if (user == null) {
                User newUser = new User();
                newUser.setEmail(onePost.getEmail());
                newUser.setName(onePost.getRealName());
                userDao.save(newUser);
                user = newUser;
            }

            Post newPost = new Post();
            newPost.setMessage(onePost.getContent());
            newPost.setUser(user);
            newPost.setCreationDate(new Date());
            postDao.save(newPost);

        } catch (Exception e) {
            return Response.serverError().build();
        }

        return Response.noContent().build();
    }

    /**
     * Get all post.
     * 
     * @return collection of all post
     */
    @GET
    @Produces("application/json")
    public PostContainer getJsonMessage() {
        PostContainer result = new PostContainer();

        Collection<Post> allPost = postDao.getAllPost();
        
        List<PostData> saveData = new ArrayList<PostData>();

        for(Post aPost : allPost) {
            PostData data = new PostData();
            data.setContent(aPost.getMessage());
            data.setEmail(aPost.getUser().getEmail());
            data.setRealName(aPost.getUser().getName());
            data.setId(aPost.getId());
            System.out.println(data.getId());
            long delay = (new Date()).getTime() - aPost.getCreationDate().getTime();
            data.setSince((delay/1000)+"s");
            saveData.add(data);
        }

        result.setAllPost(saveData);

        return result;
    }

	@POST
	@Path("/suppPost")
	public Response postAddUser(@FormParam("ident") int ident) throws URISyntaxException{
		
		System.out.println("suppPost::suppresion du post num = " + ident);
		Post aPost = postDao.findPostById(ident);
		postDao.delete(aPost);
        return Response.noContent().build();	
	}
	
    
    
    /**
     * Get User Dao.
     * 
     * @return user Dao
     */
    public UserDao getUserDao() {
        return userDao;
    }

    /**
     * Set User Dao.
     * 
     * @param userDao the user Dao
     */
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * Get Post Dao.
     * 
     * @return post Dao
     */
    public PostDao getPostDao() {
        return postDao;
    }

    /**
     * Set Post Dao.
     * 
     * @param postDao the post Dao
     */
    public void setPostDao(PostDao postDao) {
        this.postDao = postDao;
    }

}
